package lex.explorer.directory_on_treemodel;

import java.io.File;

import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.lex.swing.Swings;


public class FileTreeModel implements TreeModel
{
	public static void main( String[] args )
	{
		Swings.show( new JTree( new FileTreeModel() ) );
	}

	private File root = null;
	private FileSystemView fsv = null;
	private boolean showHidenFile = true;

	public FileTreeModel()
	{
		this( FileSystemView.getFileSystemView().getHomeDirectory() );
	}

	public FileTreeModel( File root )
	{
		this.root = root;
		this.fsv = FileSystemView.getFileSystemView();
	}

	@Override
	public Object getChild( Object parent, int index )
	{
		return fsv.getFiles( (File) parent, !showHidenFile )[index];
	}

	@Override
	public int getChildCount( Object parent )
	{
		return fsv.getFiles( (File) parent, !showHidenFile ).length;
	}

	@Override
	public int getIndexOfChild( Object parent, Object child )
	{
		return org.lex.utils.Arrays.firstEquals( fsv.getFiles( (File) parent, !showHidenFile ), child );
	}

	@Override
	public Object getRoot()
	{
		return root;
	}

	@Override
	public boolean isLeaf( Object node )
	{
		return !((File) node).isDirectory();
	}

	// not using
	@Override
	public void addTreeModelListener( TreeModelListener l )
	{}

	@Override
	public void removeTreeModelListener( TreeModelListener l )
	{}

	@Override
	public void valueForPathChanged( TreePath path, Object newValue )
	{}
}
